package com.isecta.maplew.utility;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * @author none
 */
public class Util {

    public enum CONNSTATUS {

        CONNECTED,
        DISCONNECTED,
        ALREADYCONNECTED,
        ERROR
    };
    
    public enum CLIENTS {
        CRAIGSLIST,
        NGFX
    };
    
    public static boolean connect(String ip, String uName, String password) {
        Util.CONNSTATUS status = Util.connectVPN("C", ip, uName, password);
        if (status.equals(Util.CONNSTATUS.CONNECTED)) {
            return true;
        }

        if (status.equals(Util.CONNSTATUS.ALREADYCONNECTED)) {
            status = Util.connectVPN("D", ip, uName, password);
        }

        if (status.equals(Util.CONNSTATUS.DISCONNECTED)) {
            status = Util.connectVPN("C", ip, uName, password);
        }

        if (status.equals(Util.CONNSTATUS.ERROR)) {
            status = Util.connectVPN("C", ip, uName, password);
        }

        if (status.equals(Util.CONNSTATUS.CONNECTED)) {
            return true;
        }
        return false;
    }

    public static boolean disConnect(String ip, String uName, String password) {
        try {
            Util.CONNSTATUS status = Util.connectVPN("D", ip, uName, password);
            if (status.equals(Util.CONNSTATUS.DISCONNECTED)) {
                return true;
            }

            if (status.equals(Util.CONNSTATUS.ERROR)) {
                status = Util.connectVPN("D", ip, uName, password);
            }

            if (status.equals(Util.CONNSTATUS.DISCONNECTED)) {
                return true;
            }
        } catch (Throwable ex) {
            ex.printStackTrace();
            //log.debug("disConnect:: stopped causes " + ex);
        }
        return false;
    }

    public static CONNSTATUS connectVPN(String action, String ip, String uName, String password) {
        try {
            String CMD;
            if (action.equalsIgnoreCase("C")) {
                CMD = "cmd /c rasdial.exe irobot " + uName + " " + password + " /PHONE:" + ip;
            } else {
                //CMD = "cmd /c rasphone.exe -h office";
                CMD = "cmd /c rasdial.exe irobot /DISCONNECT";
            }
            // Run "netsh" Windows command
            Process process = Runtime.getRuntime().exec(CMD);
            // Get input streams
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));

            // Read command standard output            
            String s;
            while ((s = stdInput.readLine()) != null) {
                if (s.contains("Successfully connected to")) {
                    return CONNSTATUS.CONNECTED;
                } else if (s.contains("You are already connected")) {
                    return CONNSTATUS.ALREADYCONNECTED;
                } else if (s.contains("Remote Access error")) {
                    return CONNSTATUS.ERROR;
                } else if (s.contains("Command completed successfully.")) {
                    return CONNSTATUS.DISCONNECTED;
                }
            }
        } catch (Throwable ex) {
            ex.printStackTrace();
            //log.debug("connectVPN:: stopped causes " + ex);
        }
        return CONNSTATUS.ERROR;
    }

    
}
